//
//  AW2EIMMessageStream.h   socket流
//  AtWork2
//
//  Created by 吕硕 on 15/4/8.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import <Foundation/Foundation.h>
@class AWClientStream;
@class AWMessage;
@class AWSessionMetaData;

typedef void (^MessageManage)(id theMessage, id result);

typedef NS_ENUM(NSUInteger, AW2StreamConnectionState) {
    
    AW2StreamConnectionStateConnected = 200,
    
    AW2StreamConnectionStateConnecting,
    
    AW2StreamConnectionStateDisconnected,
    
    AW2StreamConnectionStateAuthenticated,
    
    AW2StreamConnectionStateRequestConnecting
};

#pragma mark - AW2MessageStreamDelegate

@protocol AW2ClientStreamDelegate <NSObject>

@optional

- (void)streamWillConnect:(AWClientStream *)sender;    /**< 准备连接Socket服务器 */

- (void)streamDidConnect:(AWClientStream *)sender;     /**< 连接Socket服务器成功 */

- (void)stream:(AWClientStream *)sender didDisconnectWithError:(NSError*)error;    /**< Socket服务器断开*/

- (void)stream:(AWClientStream *)sender didSendMessage:(id)message withTag:(long)tag;  /**< 发送Socket数据成功 */

/**
 *  socket认证成功。可以拉取离线消息等等。
 */
- (void)streamdidAuthentication:(AWClientStream *)sender;

/**
 *  接收到信息
 *
 *  @param sender
 *  @param message 消息
 */
- (void)stream:(AWClientStream *)sender didReceiveMessage:(AWMessage *)message;

/**
 *  接收到服务器发回来的信息
 *
 *  @param sender
 *  @param data   数据
 */
- (void)stream:(AWClientStream *)sender didReceiveData:(NSData*)data;

@end

#pragma mark - AWClientStream

@interface AWClientStream : NSObject

/** 连接会话的设置  */
@property (nonatomic, strong, readonly) AWSessionMetaData *sessionMetadata;


/** 是否正常取消连接 */
@property (nonatomic, assign, readonly, getter=isNormalCancelled) BOOL normalCancelled;

/** 连接状态 */
@property (nonatomic, assign, readonly) AW2StreamConnectionState streamConnectionState;

@property (nonatomic, assign, readonly) int64_t sendingPingTimestamp;

- (instancetype)initWithAW2MessageStreamDelegate:(id<AW2ClientStreamDelegate>)delegate;

- (void)sendMessage:(id)element withTag:(long)tag;

- (void)sendMessage:(AWMessage *)message;

/**
 *  正常取消, IM Socket 不会重连。
 */
- (void)normalCancelled;

/**
 *  非正常取消，IM Socket 会自动重连。
 */
- (void)abnormalCancelled;

/** 断开连接 */
- (void)disConnection;

/** 建立连接 */
- (void)connection;

/**
 *  根据会话元数据建立连接。
 */
- (void)connectWithSessionMedaData:(AWSessionMetaData *)sessionMetaData;

@end
